<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="月份" prop="resultMonth">
        <!--        <el-date-picker clearable size="small"-->
        <!--                        v-model="queryParams.resultMonth"-->
        <!--                        type="month"-->
        <!--                        value-format="yyyy-MM"-->
        <!--                        placeholder="选择月份">-->
        <!--        </el-date-picker>-->
        <el-date-picker
          v-model="daterangeFaultTime"
          type="monthrange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属仓库" prop="depotId">
        <el-select v-model="queryParams.depotId" clearable size="small">
          <el-option
            v-for="depot in depotOptions"
            :key="depot.id"
            :label="depot.name"
            :value="depot.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis:stockMonth:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis:stockMonth:edit']"
          >修改</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['analysis:stockMonth:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis:stockMonth:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stockMonthList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="月份"
        align="center"
        prop="resultMonth"
        width="180"
      >
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.resultMonth, '{y}-{m}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="所属仓库" align="center" prop="depotName" />
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="库存种类" align="center" prop="typeNum" />
      <el-table-column label="库存数量" align="center" prop="stockNum" />
      <el-table-column label="总金额" align="center" prop="stockMoney" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['analysis:stockMonth:edit']"
            >修改</el-button
          >
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['analysis:stockMonth:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改每月库存分析对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
      fullscreen
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="月份" prop="resultMonth">
                <el-input v-model="form.resultMonth" :disabled="openDisabled" />
                <!--                <el-date-picker clearable size="small"-->
                <!--                                v-model="form.resultMonth"-->
                <!--                                type="date"-->
                <!--                                value-format="yyyy-MM-dd"-->
                <!--                                placeholder="选择月份" :disabled="openDisabled">-->
                <!--                </el-date-picker>-->
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属仓库" prop="depotId">
                <el-select v-model="form.depotId" :disabled="openDisabled">
                  <el-option
                    v-for="depot in depotOptions"
                    :key="depot.id"
                    :label="depot.name"
                    :value="depot.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存种类" prop="typeNum">
              <el-input
                v-model="form.typeNum"
                placeholder="请输入种类"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存数量" prop="stockNum">
              <el-input
                type="number"
                v-model="form.stockNum"
                placeholder="请输入库存数量"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总金额" prop="stockMoney">
              <el-input
                type="number"
                v-model="form.stockMoney"
                placeholder="请输入总金额"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="24">-->
          <!--            <el-form-item label="备注" prop="remark">-->
          <!--              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStockMonth,
  getStockMonth,
  delStockMonth,
  addStockMonth,
  updateStockMonth,
} from "@/api/report/analysis/stockMonth";
import { listDepot, getDepot } from "@/api/equipment/materials/depot";
export default {
  name: "StockMonth",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 每月库存分析表格数据
      stockMonthList: [],
      // 所属仓库
      depotOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        resultMonth: null,
        depotId: null,
        code: null,
        typeNum: null,
        stockNum: null,
        stockMoney: null,
        beginFaultTime: null,
        endFaultTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "本月",
            onClick(picker) {
              picker.$emit("pick", [new Date(), new Date()]);
            },
          },
          {
            text: "今年至今",
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近六个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      daterangeFaultTime: [],
    };
  },
  created() {
    this.getList();
    this.getDepot();
  },
  methods: {
    resetDepot() {
      this.depotParams = {
        type: null,
        parentId: null,
        id: null,
      };
    },
    getDepot() {
      this.resetDepot();
      this.depotParams.type = "01";
      listDepot(this.depotParams).then((response) => {
        this.depotOptions = response.data;
      });
    },
    /** 查询每月库存分析列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangeFaultTime && "" != this.daterangeFaultTime) {
        this.queryParams.beginFaultTime = this.daterangeFaultTime[0];
        this.queryParams.endFaultTime = this.daterangeFaultTime[1];
      } else {
        this.queryParams.beginFaultTime = null;
        this.queryParams.endFaultTime = null;
      }
      listStockMonth(this.queryParams).then((response) => {
        this.stockMonthList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        resultMonth: null,
        depotId: null,
        code: null,
        typeNum: null,
        stockNum: null,
        stockMoney: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加每月库存分析";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getStockMonth(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改每月库存分析";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getStockMonth(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看每月库存分析";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStockMonth(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addStockMonth(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除每月库存分析编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delStockMonth(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "report/stockMonth/exportSheets",
        {
          ...this.queryParams,
        },
        "每月库存分析" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
